<?php
namespace Admin\Controller;
use Think\Controller;
class WriteController extends CommonController {

	//代写服务列表
    public function writeList(){

        C('TOKEN_ON',false);
        
        $map=I("get.");

        if($map["secuname"]){
            $where["cloud_user.username"]=array("like","%".$map["secuname"]."%");
        }
        if($map["sectitle"]){
            $where["cloud_write.title"]=array("like","%".$map["sectitle"]."%");
        }
        if($map["secstatus"]){
            $where["cloud_write.is_apply"]=$map["secstatus"];
        }

        $writeModel=D("write");

        $comList= $writeModel->getWriteList($where);
       
        $this->assign("writeList",$comList["comList"]);
        $this->assign("page",$comList["page"]);

        $status=getWriteStatus();
        $this->assign("apply_status",$status["apply"]);
        $this->assign("ok_status",$status["ok"]);

        $this->display();
    }
    //代写服务详情
    public function writeDetail(){

        $id=I("get.id");

        if($id>0){

            $write=D("write")->getWriteById($id);
        
            $this->assign("write",$write);
            $this->display();
        }else{
            $this->error("参数错误");
        }
    }

   
    //ajax审核代写服务
    public function ajaxChangeWrite(){

        $map=I("post.");

        $arr['code'] = 100;
        $arr['info'] = "操作失败";

        if(IS_AJAX && $map) {
            
            $res=D("write")->changeData($map);
            if($res){
                $arr['code'] = 200;
                $arr['info'] = "操作成功";
            }
        };
        $this->ajaxReturn($arr);

    }
    //添加、修改代写套餐
    public function editPackage(){

        $id=I("get.id");

        if($id > 0){
            $package=D("writeTypePackage")->getPackageById($id);
            $this->assign("package",$package);   
        }

        $this->display();
    }
    //添加、修改代写套餐表单
    public function editPackageForm(){

        $data=I("post.");

        $packageModel=D("writeTypePackage");
        $res=$packageModel->editData($data);

        if($res){
            $this->success();
        }else{
            $this->error($packageModel->getError(),$_SERVER['HTTP_REFERER']);
        }

    }
    //代写套餐
    public function writePackage(){

        $map=I("get.");
        if($map["sectitle"]){
            $where["title"]=array("like","%".$map["sectitle"]."%");
        }
        $comList=D("writeTypePackage")->getWriteTypePackageList($where);
        
        $this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]);

        $this->display();
    }

    //ajax删除套餐
    public function ajaxDelPackage(){

        $map=I("post.ids");

        $arr['code'] = 100;
        $arr['info'] = "删除失败";

        if(IS_AJAX && $map) {
            
            //拆分id
            $data=explode(",",$map);

            $packageModel=D("writeTypePackage");
            $res=$packageModel->delData($data);

            if($res>0){
                $arr['code'] = 200;
                $arr['info'] = "删除成功";
            }
        };
        $this->ajaxReturn($arr);

    } 

    //添加、修改代写类型
    public function editType(){

        $id=I("get.id");

        if($id > 0){
            $type=D("writeType")->getTypeById($id);
            $this->assign("type",$type);
        }

        $this->display();
    }
    //添加、修改代写类型表单
    public function editTypeForm(){

        $data=I("post.");

        $typeModel=D("writeType");
        $res=$typeModel->editData($data);

        if($res){
            $this->success();
        }else{
            $this->error($typeModel->getError(),$_SERVER['HTTP_REFERER']);
        }

    }
    //代写类型
    public function writeType(){

        $map=I("get.");
        if($map["sectitle"]){
            $where["title"]=array("like","%".$map["sectitle"]."%");
        }
        $comList=D("writeType")->getWriteTypeList($where);
        
        $this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]);

        $this->display();
    }

    //ajax删除类型
    public function ajaxDelType(){

        $map=I("post.ids");

        $arr['code'] = 100;
        $arr['info'] = "删除失败";

        if(IS_AJAX && $map) {
            
            //拆分id
            $data=explode(",",$map);

            $typeModel=D("writeType");
            $res=$typeModel->delData($data);

            if($res>0){
                $arr['code'] = 200;
                $arr['info'] = "删除成功";
            }
        };
        $this->ajaxReturn($arr);

    }
}